(ns ca.uoit.molly.tests.query-tests
  (:use [ca.uoit.molly.db-molly]
        [ca.uoit.molly.lucene-helper]
        [ca.uoit.molly.lucene-molly]
        [ca.uoit.molly.mycampus :as mycampus]
        [ca.uoit.molly.entity-tree])
 (:require [ca.uoit.molly.search-api :as api]))

(println "Test 1: low level query a course")
(let [q (mk-simple-query :or (mk-analyzer) "__all__" "database 2010")
      idx (mk-index-searcher "mycampus-entity.idx")]
  (doseq [doc (index-search idx q 10) :let [ent (doc-to-entity doc)]]
    (println ent))
  (close-index-searcher idx))

(println "Test 2: low level query a value")
(let [q (mk-simple-query :or (mk-analyzer) "__all__" (ngram-str "database 2010"))
      idx (mk-index-searcher "mycampus-value.idx")]
  (doseq [doc (index-search idx q 10) :let [value (doc-to-value doc)]]
    (println value))
  (close-index-searcher idx))

(println "Test 3: search entity")
(let [idx (mk-index-searcher "mycampus-entity.idx")]
  (doseq [ent (search-entities idx "uoit ua1350" 10)]
    (println ent))
  (close-index-searcher idx))

(println "Test 4: search value")
(let [idx (mk-index-searcher "mycampus-value.idx")]
  (doseq [v (search-values idx "database 2010" 10)]
    (println (format "%s/%s/%s" (v :entity) (v :attr) (v :value))))
  (close-index-searcher idx))

(println "Test 5: search group")
(let [idx (mk-index-searcher "mycampus-group.idx")]
  (println "Search results of \"instructor/1\"")
  (doseq [doc (search-groups idx 5 {:entity "instructor/1"})]
    (println doc)
    (println (doc-to-denorm-group doc))
    (println "-----------------------"))
  (println "Search results of \"fall 2006\"")
  (doseq [doc (search-groups idx 5 {:param "fall 2006"})]
    (print-tree (apply build-tree (doc-to-denorm-group doc)))
    (println "-----------------------")))

(println "Test 6: search API for entities")
(let [idx (mk-index-searcher "mycampus-entity.idx")]
  (doseq [ent (api/search-entities idx 10 "uoit ua1350")]
    (println ent))
  (close-index-searcher idx))

